const openid = getApp().globalData.openid;

Page({
    data: {
        //选中的素菜列表
        vegetable: [],
        vegetableNum: 0,
        //选中的荤菜列表
        meat: [],
        meatNum: 0,
        //选中的汤列表
        soap: [],
        soapNum: 0,
        //显示结果框
        showResult: "hidden",
        //显示开始按钮
        showStart: "",
        //显示点击开始动画
        animation: "",
    },

    //点击加减更新要随机的份数
    update(e) {
        switch (e.detail[0]) {
            case "素":
                this.setData({
                    vegetableNum: e.detail[1]
                });
                break;
            case "荤":
                this.setData({
                    meatNum: e.detail[1]
                });
                break;
            case "汤":
                this.setData({
                    soapNum: e.detail[1]
                });
                break;
        }
    },

    //点击按钮开始随机
    start(e) {
        if(!this.data.vegetableNum && !this.data.meatNum && !this.data.soapNum) {
            wx.showToast({
              icon: 'error',
              title: '请先进行选择',
            })
            return;
        }
        this.setData({
            animation: "click 1s ease-in-out forwards"
        });
        var that = this;
        setTimeout(function () {
            that.setData({
                showResult: "",
                showStart: "hidden"
            });
            that.getRandom();
        }, 1300);
    },

    //点击确定将随机结果保存到结果列表
    confirm(e) {
        //创建结果数组
        let randomChoosed = [];
        if(this.data.vegetable) {
            this.data.vegetable.forEach((item,index) => {
                randomChoosed.push(item);
            });
        }
        if(this.data.meat) {
            this.data.meat.forEach((item,index) => {
                randomChoosed.push(item);
            });
        }
        if(this.data.soap) {
            this.data.soap.forEach((item,index) => {
                randomChoosed.push(item);
            });
        }

        //将结果保存至本地缓存和云数据库
        wx.setStorageSync('randomMenuChoosed', randomChoosed);
        wx.cloud.database().collection("randomMenuChoosed").add({
            data: {
                _id: openid,
                randomMenuChoosed: randomChoosed
            },
            success: res => {
                console.log("上传随机结果至服务器成功")
            },
            fail: err => {
                console.log("上传随机结果至服务器失败" + err);
            }
        });

        wx.showToast({
          title: '添加备忘录成功',
        })
    },

    //点击重随重新随机
    tryAgain(e) {
        this.setData({
            vegetable: [],
            meat: [],
            soap: [],
        })
        this.getRandom();
    },

    //随机一次
    getRandom() {
        var that = this;
        var list = wx.getStorageSync("menuList") ? wx.getStorageSync("menuList") : wx.getStorageSync("defaultMenuList");

        //每半秒产生一个素菜的随机结果
        if (this.data.vegetableNum > 0) {
            (function () {
                let max = list[0].length,
                    i = 0,
                    index = [parseInt(Math.random() * max)];
                let getsu = setInterval(function () {
                    //保存这次循环的随机结果
                    that.data.vegetable.push(list[0][index[index.length - 1]]);
                    that.setData({
                        vegetable: that.data.vegetable
                    })
                    //产生下一个随机数
                    do {
                        var temp = parseInt(Math.random() * max);
                    } while (index.indexOf(temp) >= 0);
                    index.push(temp);
                    //清除定时器
                    if (++i == that.data.vegetableNum) {
                        clearInterval(getsu);
                    }
                }, 500);
            })();
        }

        //每半秒产生一个荤菜的随机结果
        if (this.data.meatNum > 0) {
            (function () {
                let max = list[1].length,
                    i = 0,
                    index = [parseInt(Math.random() * max)];
                let gethun = setInterval(function () {
                    //保存这次循环的随机结果
                    that.data.meat.push(list[1][index[index.length - 1]]);
                    that.setData({
                        meat: that.data.meat
                    })
                    //产生下一个随机数
                    do {
                        var temp = parseInt(Math.random() * max);
                    } while (index.indexOf(temp) >= 0);
                    index.push(temp);
                    //清除定时器
                    if (++i == that.data.meatNum) {
                        clearInterval(gethun);
                    }
                }, 500);
            })();
        }

        //每半秒产生一个汤的随机结果
        if (this.data.soapNum > 0) {
            (function () {
                let max = list[2].length,
                    i = 0,
                    index = [parseInt(Math.random() * max)];
                let gettang = setInterval(function () {
                    //保存这次循环的随机结果
                    that.data.soap.push(list[2][index[index.length - 1]]);
                    that.setData({
                        soap: that.data.soap
                    })
                    //产生下一个随机数
                    do {
                        var temp = parseInt(Math.random() * max);
                    } while (index.indexOf(temp) >= 0);
                    index.push(temp);
                    //清除定时器
                    if (++i == that.data.soapNum) {
                        clearInterval(gettang);
                    }
                }, 500);
            })();
        }
    },

})